package com.xhh.check.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.check.mapper.CheckMapper;
import com.xhh.check.mapper.IdentityMapper;
import com.xhh.check.mapper.UnityMapper;
import com.xhh.check.service.CheckService;
import com.xhh.check.service.UnityService;
import com.xhh.model.check.dtos.GetUnityListDto;
import com.xhh.model.check.pojos.Identity;
import com.xhh.model.check.pojos.Unity;
import com.xhh.model.check.pojos.UserIdentity;
import com.xhh.model.check.vos.CheckedVo;
import com.xhh.model.common.dtos.ResponseResult;
import com.xhh.model.common.enums.AppHttpCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UnityServiceImpl extends ServiceImpl<UnityMapper, Unity> implements UnityService {

    /**
     * 根据区域和省市编码获取招生单位列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getUnityListByZoneAndPCC(GetUnityListDto dto) {
        // 1. 构建查询条件 默认查全部
        List<Unity> list = lambdaQuery()
                .eq(StrUtil.isNotEmpty(dto.getZone()), Unity::getZone, dto.getZone())
                .eq(StrUtil.isNotEmpty(dto.getPCC()), Unity::getProvinceCityCode, dto.getPCC())
                .list();
        if (CollUtil.isEmpty(list)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 2. 返回结果
        return ResponseResult.okResult(list);
    }

    /**
     * 根据单位编码获取单位信息
     * @param uCode
     * @return
     */
    @Override
    public ResponseResult getUnityByCode(String uCode) {
        // 1. 校验参数
        if (StrUtil.isEmpty(uCode)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 构造查询
        Unity unity = lambdaQuery()
                .eq(Unity::getUnityCode, uCode)
                .one();
        if (unity == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(unity);
    }

}
